import secrets

import rsa

import sys
import os

from backend.DID.BlockChain import insert_into_blockchain, delete_repetitive_key

sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../../')))
from backend.DID.Identity_Management import hashFunction




def generate_pair_key():
    publicKey,privateKey = rsa.newkeys(1024)
    publicKey_pem = publicKey.save_pkcs1().decode("UTF-8")
    privateKey_pem = privateKey.save_pkcs1().decode("UTF-8")
    return publicKey_pem,privateKey_pem


def generate_aes_key():
    return secrets.token_bytes(16)

#上传公钥进入区块链两种方法
#1
def upload_public_key_to_blockchain(benji_kuaji_information,temporary_public_key):
    try:
        table_name = "temporary_key_other_to_me"
        benji_information = benji_kuaji_information[:3]
        kuaji_information = benji_kuaji_information[3:]
        benji_hash1 = hashFunction(benji_information)
        kuaji_hash1 = hashFunction(kuaji_information)
        insert_data = {
            'my_hash1':f'{benji_hash1}',
            'other_hash1':f'{kuaji_hash1}',
            'temporary_key_other_to_me':f'{temporary_public_key}'
        }
        insert_into_blockchain(table_name,insert_data)
    except Exception as e:
        print(f"DID部分上传临时公钥出错，错误为{e}")

#2
def upload_temporary_public_key(benji_hash1,kuaji_hash1,temporary_public_key):
    bc_temporary_table_name = "temporary_key_other_to_me"
    temporary_data = {
        'my_hash1':f'{benji_hash1}',
        'other_hash1':f'{kuaji_hash1}',
        'temporary_PublicKey':f'{temporary_public_key}'
    }
    try:
        insert_into_blockchain(bc_temporary_table_name,temporary_data)
    except Exception as e:
        print(f"DID部分上传临时公钥出错，错误为{e}")


"""本机存储给别人临时私钥"""
def b_insert_temporary_private_key(other_did,temporary_private_key):
    from backend.DID.DataBase import insert_data
    db_path = 'did.db'
    table_name = "temporary_key_to_other"
    data = {
        'other_id': other_did,
        'temporary_PrivateKey': temporary_private_key
    }
    try:
        insert_data(db_path,table_name,data)
    except Exception as e:
        print(f"DID部分本机存储临时私钥出错，错误为：{e}")


"""本机存储临时公钥进入区块链"""



def store_temporary_PublicKey(my_did, other_did, temporary_PublicKey):
    table_name = "temporary_key_other_to_me"
    data = {
        'my_id': my_did,
        'other_id': other_did,
        'temporary_PublicKey': temporary_PublicKey
    }
    try:
        # 无论如何都先尝试删除可能存在的重复记录
        delete_repetitive_key(my_did, other_did)

        # 然后插入新记录
        result = insert_into_blockchain(table_name, data)
        if result == 1:
            return 1
        else:
            return 0
    except Exception as e:
        print(f"DID部分 插入临时公钥出错，错误为{e}")
        return 0




"""跨机存储别人给自己的临时私钥"""
def k_insert_temporary_private_key(other_did,temporary_private_key):
    from backend.DID.DataBase import insert_data
    db_path = 'did.db'
    table_name = "temporary_key_other_to_me"
    data = {
        'other_id':other_did,
        'temporary_PrivateKey':temporary_private_key
    }
    try:
        if insert_data(db_path,table_name,data):
            return 1
        else:
            return 0
    except Exception as e:
        print(f"DID部分 跨机存储临时私钥出错，错误为：{e}")
        return 0
